<template>
    <picker
        :class="[classes, { disabled: disabled }]"
        :style="[styles]"
        class="date-picker"
        mode="date"
        :start="start"
        :end="end"
        :fields="fields"
        :value="value11"
        :disabled="disabled"
        @change="handleChange"
        @cancel="$emit('cancel', { detail: { value: value1 } })"
    >
        <slot></slot>
    </picker>
</template>

<script>
import baseMixin from '../mixins/base.js'
import formMixin from '../mixins/form.js'

export default {
    name: 'uxt-date-picker',
    mixins: [baseMixin, formMixin],
    props: {
        value: {
            type: String,
            default: ''
        },
        start: {
            type: String,
            default: ''
        },
        end: {
            type: String,
            default: ''
        },
        fields: {
            type: String,
            default: 'day',
            validator(value) {
                return ['year', 'month', 'day'].includes(value)
            }
        },
        bgColor: {
            default: 'white'
        }
    },
    data() {
        return {
            value11: this.getValue()
        }
    },
    watch: {
        value(newVal, oldVal) {
            this.value1 = this.getValue()
            this.value11 = this.getValue()
        }
    },
    methods: {
        getValue() {
            return this.value
        },
        handleChange(e) {
            this.value1 = e.detail.value
            this.$emit('change', { detail: { value: this.value1 } })
            this.$emit('input', this.value1)
        },
        reset(value) {
            if (this.value1 !== value) {
                this.value1 = value
                this.$emit('change', { detail: { value: this.value1 } })
                this.$emit('input', this.value1)
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.date-picker {
    position: relative;
}
</style>
